﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeviceDefectManager_WCG.Models;

namespace DeviceDefectManager_WCG.Controllers
{
    public class HomeController : Controller
    {
        Model1 model = new Model1();
        public ActionResult Index(string Content = "", int DeviceID = 0, int DefectState = 0)
        {
            var device = model.Device.ToList();
            //在用户列表下拉框首选位置添加全部选项
            device.Insert(0, new Device() { DeviceName = "全部", DeviceID = 0 });


            //填充下拉框
            ViewBag.DeviceID = new SelectList(device, "DeviceID", "DeviceName");
            //条件查询
            var list = model.Defect.Where(p => (DeviceID == 0 || p.DeviceID == DeviceID) && (p.Content.Contains(Content)) && (DefectState == 0 || p.DefectState == DefectState));
            return View(list);
        }

        public ActionResult Create()
        {
            //填充下拉框
            ViewBag.DeviceID = new SelectList(model.Device, "DeviceID", "DeviceName");

            return View();
        }
        [HttpPost]
        public ActionResult Create(Defect defect)
        {
            if (ModelState.IsValid)
            {
                //特殊属性后台赋值，不允许用户手动
                defect.AddTime = DateTime.Now;
                defect.DefectState = 1;
                defect.DealTime = null;

                model.Defect.Add(defect);
                var res = model.SaveChanges();
                if (res > 0)
                {
                    //如果添加成功返回主页
                    return RedirectToAction("Index");
                }
            }
            //填充下拉框
            ViewBag.DeviceID = new SelectList(model.Device, "DeviceID", "DeviceName");
            return View();
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id">缺陷编号</param>
        /// <returns>查询到的数据</returns>
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            // 查询数据
            var list = model.Defect.FirstOrDefault(p => p.DefectID == id);
            if (list == null)
            {
                return RedirectToAction("Index");
            }

            return View(list);
        }
        [HttpPost]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return Json(false);
            }
            // 查询数据
            var list = model.Defect.FirstOrDefault(p => p.DefectID == id);
            if (list == null)
            {
                return Json(false);
            }
            //执行删除
            model.Defect.Remove(list);
            //保存至数据库
            var res = model.SaveChanges() > 0;

            return Json(res);
        }
        [HttpPost]
        public ActionResult Edit(int? id)
        {

            //查询需要修改的数据
            var temp = model.Defect.FirstOrDefault(p => p.DefectID == id);
            if (temp == null)
            {
                return Json(false);
            }
            temp.DefectState = 2;
            temp.DealTime = DateTime.Now;
            //将更改保存至数据库
            var res = model.SaveChanges();
            if (res > 0)
                //如果更改成功返回false
                return Json(res);

            return Json(false);
        }
    }

}